package com.peanut.web.views;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.springframework.web.servlet.view.document.AbstractJExcelView;

import com.peanut.util.web.WebUtil;

public class SingleSheetExcelView extends AbstractJExcelView {
	protected void buildExcelDocument(Map model, WritableWorkbook wb,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		WritableSheet sheet = wb.createSheet(model.get("title")+"", 0);
		//导出表头
		List headerList = (List) model.get("headerList");
		for (int i = 0; i < headerList.size(); i++) {
			sheet.addCell(new Label(i, 0, (String) headerList.get(i)));
		}
		//导出数据
		List dataList = (List) model.get("dataList");
		for (int rows = 0; rows < dataList.size(); rows++) {
			for (int cols = 0; cols < headerList.size(); cols++) {
				sheet.addCell(new Label(cols, rows + 1, (String) dataList.get(rows)));
			}
		}
		String filename = model.get("filename")+".xls";//设置下载时客户端Excel的名称     
        filename = WebUtil.encodeFilename(filename, request);//处理中文文件名  
        response.setContentType("application/vnd.ms-excel");     
        response.setHeader("Content-disposition", "attachment;filename=" + filename);     
	}
}
